//封装分类数据业务相关代码
import {onBeforeRouteUpdate} from 'vue-router'
import { getCategoryAPI } from '@/apis/category'
import { useRoute } from 'vue-router'
import { ref, onMounted } from 'vue'
export function useCategory() {
const route = useRoute()
//获取分类数据
const categoryData = ref([])
//如果不传参数，使用路由默认参数
const getCategory = async (id=route.params.id) => {
    const res = await getCategoryAPI(id)
    categoryData.value = res.result
}
onMounted(() => {
    getCategory()
})


//目标：路由参数变化的时候，重新获取分类数据
onBeforeRouteUpdate((to)=>{
    getCategory(to.params.id)
})

return {
    categoryData
}
}